﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Control;
using Model;

namespace BioBooking
{
    public partial class GUIBooking : Form
    {
        private BookingController bookingCtr;
        private CustomerController custCtr;
        private List<Label> seatIdsChoicen;
        private CinemaHallPanels cHallPanels;


        public GUIBooking()
        {
            seatIdsChoicen = new List<Label>();
            bookingCtr = new BookingController();
            custCtr = new CustomerController();
            InitializeComponent();
            Init();
            cHallPanels = new CinemaHallPanels(this);
        }

        public ListBox DateListBox
        {
            get {return dateListBox ;}
        }
        public ListBox ShowListBox
        {
            get { return showListBox; } 
        }

        public List<Label> SeatIdsChoicen
        {
            get { return seatIdsChoicen; }
        }


        public void Init()
        {
            foreach(DateTime dateObj in bookingCtr.GetDates14days())
            {
                dateListBox.Items.Add(dateObj);
            }
            // SLÅR RABAT ORDNING FRA, da denne ikke bruges pt
            studRadio.Enabled = false;
            senorRadio.Enabled = false;
            specialdisRadio.Enabled = false;

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click_1(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click_2(object sender, EventArgs e)
        {

        }

        private void label2_Click_3(object sender, EventArgs e)
        {

        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void dateListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            showListBox.Items.Clear();
            foreach (Show showObj in bookingCtr.GetShowsForThisDate((DateTime)dateListBox.SelectedItem))
            {
                showListBox.Items.Add(showObj);
            }

            if (showListBox.SelectedIndex == -1)
            {
                // set label with show.
                sumShowTLabel.Text = "Ikke angivet!";
                sumShowTLabel.BackColor = Color.Red;
                
            }

            //Set label with date.
            sumDateTLabel.BackColor = Color.Transparent;
            sumDateTLabel.Text = ((DateTime)dateListBox.SelectedItem).ToString("dd/MM/yyyy");

            ResetSeatsChoicen();

            //Set label with no of tickets
            sumNoOfTicketsTLabel.Text = "Ikke angivet!";
            sumNoOfTicketsTLabel.BackColor = Color.Red;
        }

        private void ResetSeatsChoicen()
        {
            foreach (Label seatLabel in seatIdsChoicen)
            {
                seatLabel.BackColor = Color.Transparent;
            }

            seatIdsChoicen = new List<Label>();

        }

        private void payResButton_Click(object sender, EventArgs e)
        {

        }

        private void bookRadio_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void showListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            sumPriceTLabel.Text = "";
            //Set label with date
            if (showListBox.SelectedItem != null)
            {
                sumShowTLabel.BackColor = Color.Transparent;
                sumShowTLabel.Text = showListBox.SelectedItem.ToString();

                Show showObj = (Show)showListBox.SelectedItem;
                string cName = showObj.CinemaHall.Name;

                if(cName.Equals("Sal A"))
                {
                    SetPanelCinemaHallA();
                    
                }
                if(cName.Equals("Sal B"))
                {
                    SetPanelCinemaHallB();
                }
                cHallPanels.SetSeatsTaken((Show)showListBox.SelectedItem, true);

            }

            ResetSeatsChoicen();
            sumTicketTLabel.Text = "Ikke angivet!";
            sumTicketTLabel.BackColor = Color.Red;
            sumNoOfTicketsTLabel.Text = "Ikke angivet!";
            sumNoOfTicketsTLabel.BackColor = Color.Red;
            
        }

        public void UpdateLabels(string oldLetter, string newLetter)
        {
            if (seatIdsChoicen.Count == 0)
            {
                sumTicketTLabel.Text = "Ikke angivet!";
                sumTicketTLabel.BackColor = Color.Red;
                sumNoOfTicketsTLabel.Text = "Ikke angivet!";
                sumNoOfTicketsTLabel.BackColor = Color.Red;

                sumPriceTLabel.Text = "";
            }
            else
            {
                double priceShowTicket = ((Show)showListBox.SelectedItem).PricePerTicket;
                double priceTotal = 0;
                string seats = "";
                foreach (Label seatLabel in seatIdsChoicen)
                {
                    seats = seats + seatLabel.Name.Replace(oldLetter, newLetter + "-") + ", ";
                }

                priceTotal = priceShowTicket * seatIdsChoicen.Count;;

                sumTicketTLabel.Text = seats;
                sumTicketTLabel.BackColor = Color.Transparent;

                sumNoOfTicketsTLabel.Text = "" + seatIdsChoicen.Count;
                sumNoOfTicketsTLabel.BackColor = Color.Transparent;

                sumPriceTLabel.Text = "" + priceTotal;
            }

        }

        private void panel5_Paint(object sender, PaintEventArgs e)
        {

        }

        private void SetPanelCinemaHallA()
        {
            this.panel5.Controls.Clear();
            this.panel5.Controls.Add(cHallPanels.PanelA);
        }

        private void SetPanelCinemaHallB()
        {
            this.panel5.Controls.Clear();
            this.panel5.Controls.Add(cHallPanels.PanelB);
        }

        private void bookRadio_CheckedChanged_1(object sender, EventArgs e)
        {
            if (bookRadio.Checked == true)
            {
                sumOrderFormTLabel.Text = "Køb";
                sumOrderFormTLabel.BackColor = Color.Transparent;
            }
        }

        private void reservationRadio_CheckedChanged(object sender, EventArgs e)
        {
            if (reservationRadio.Checked == true)
            {
                sumOrderFormTLabel.Text = "Reservering";
                sumOrderFormTLabel.BackColor = Color.Transparent;
            }
        }

        private void nameTxtBox_TextChanged(object sender, EventArgs e)
        {
            SumCustLabelTUpdate();
        }

        private void phoneTxtBox_TextChanged(object sender, EventArgs e)
        {
            SumCustLabelTUpdate();
        }

        private void SumCustLabelTUpdate()
        {
            if (nameTxtBox.Text.Length > 0 || phoneTxtBox.Text.Length > 0)
            {
                sumCustTLabel.BackColor = Color.Transparent;
                sumCustTLabel.Text = nameTxtBox.Text + ", " + phoneTxtBox.Text;
            }
            else
            {
                sumCustTLabel.BackColor = Color.Red;
                sumCustTLabel.Text = "Ikke angivet!";
            }
        }

        private void payResButton_Click_1(object sender, EventArgs e)
        {
            if (seatIdsChoicen.Count > 0)
            {
                if (bookRadio.Checked == true || reservationRadio.Checked == true)
                {
                    if (nameTxtBox.Text.Length > 0 && phoneTxtBox.Text.Length > 0)
                    {
                        try
                        {
                            Convert.ToInt32(phoneTxtBox.Text);
                            CreateBooking();
                        }
                        catch (Exception exp)
                        {
                            Console.WriteLine(exp.Message);
                        }

                    }
                    else
                    {
                        MessageBox.Show("Navn eller telefonnummer mangler.");
                    }
                }
                else
                {
                    MessageBox.Show("Du skal vælge om dette er et køb eller en reservation.");
                }
            }
            else
            {
                MessageBox.Show("Du mangler at vælge dato, forestilling eller sæder.");
            }
        }

        private void CreateBooking()
        {
            List<string> seats = new List<string>();
            string cLetter = seatIdsChoicen[0].Name.Substring(0,1);

            Console.WriteLine("cletter: " + cLetter);

            string newLetter = "";
            if(cLetter.Equals("a"))
            {
                newLetter = "A";
            }
            if(cLetter.Equals("b"))
            {
                newLetter = "B";
            }

            foreach(Label seatLabel in seatIdsChoicen)
            {
                seats.Add(seatLabel.Name.Replace(cLetter, newLetter + "-"));
            }


            DateTime dt = ((Show)showListBox.SelectedItem).DateTimeStart;
            string cName =((Show)showListBox.SelectedItem).CinemaHall.Name;
            double price =((Show)showListBox.SelectedItem).PricePerTicket;


            int reservation = -1;
            if(reservationRadio.Checked == true)
            {
                reservation = 1;
            }
            if(bookRadio.Checked == true)
            {
                reservation = 0;
            }

            if (custCtr.FindCustomer(phoneTxtBox.Text) == null)
            {
                Console.WriteLine("LOL JESPER");
                custCtr.CreateCustomer(nameTxtBox.Text, "", phoneTxtBox.Text, "", "Normal");
            }


            int rc = bookingCtr.CreateBooking(seats, dt, cName, phoneTxtBox.Text, reservation, price);
            if (rc == 1)
            {
                if (reservation == 0)
                {
                    MessageBox.Show("Køb er valgt - Betaling.");
                }
                else
                {
                    MessageBox.Show("Reservation oprettet.");
                }
                this.Dispose();
            }
        }

    }
}
